/*
 * @Author: Binhuang4
 * @Date: 2019-06-21 16:27:46
 * @LastEditors: Binhuang4
 * @LastEditTime: 2019-06-27 09:46:37
 * @Description: file content
 */
package main

import (
	"fmt"
	"time"
)

const timeFormat = "2006-01-02 15:05:06"

type Product struct {
	ID   int
	Name string
	Time string
}

type Seller struct {
	ID int
	Product
}

func main() {
	var productChan = make(chan Product, 10)
	var sellChan = make(chan Seller, 10)
	go produce(productChan)
	go sell(productChan, sellChan)
	for {
		if len(sellChan) >= 10 {
			break
		}
	}
	close(productChan)
	close(sellChan)
}

func produce(productChan chan<- Product) { //chan<- Product 表示 只写的chan Product
	for i := 1; i <= 10; i++ {
		name := fmt.Sprintf("my name is %d Product", i)
		var product = Product{i, name, time.Now().Format(timeFormat)}
		time.Sleep(time.Millisecond * 500)
		fmt.Println(product)
		productChan <- product
	}
}

func sell(productChan <-chan Product, sellerChan chan<- Seller) {
	for i := 1; i <= 10; i++ {
		seller := Seller{i, <-productChan}
		fmt.Println(seller)
		sellerChan <- seller
	}
}
